const app = getApp()
var util = require('../../utils/util.js')

Page({
 
  /**
   * 页面的初始数据
   */
  data: {
     
  },

  // 跳转到添加地址
  toAddAddress: function (e) {
    // let id = e.currentTarget.id
    wx.navigateTo({
         url: '../add-address/add-address?cartIds=' + this.data.cartIds + '&cartType=' + this.data.cartType
    })
  },
   editAddAddress:function(e){
        wx.redirectTo({
             url: '../add-address/add-address?id=' + e.currentTarget.id + '&cartIds=' + this.data.cartIds + '&cartType=' + this.data.cartType
        })
   },
     deleteAddAddress: function (e){
          let addresId = e.currentTarget.id
          let that = this
          wx.showModal({
               title: '提示',
               content: '是否删除',
               success: function (res) {
                    if (res.confirm) {

                         wx.request({
                              url: app.globalData.baseURL+'/address/delete',
                              method: "POST",
                              header: {
                                   "Content-Type": "application/x-www-form-urlencoded"
                              },
                              data: {
                                   "addressId": addresId
                              },
                              success: function (res) {
                                   that.loadAddress()
                              }
                         })

                    } else if (res.cancel) {

                    }
               }
          })
    },
     setDefault: function (e) {
          let addresId = e.currentTarget.id
          let that = this
          wx.request({
               url: app.globalData.baseURL +'/address/setDefault',
               method: "POST",
               header: {
                    "Content-Type": "application/x-www-form-urlencoded"
               },
               data: {
                    "addressId": addresId,
                    "memberId": app.globalData.userInfo.id
               },
               success: function (res) {
                    if (res.data.code == 0) {
                         that.loadAddress()
                    }
               }
          })

     },
     //选择地址返回
     selectAddress: function (e) {
          wx.redirectTo({
               url: '../confir-order/confir-order?cartIds=' + this.data.cartIds + '&addressId=' + e.currentTarget.id + '&cartType=' + this.data.cartType
          })
     },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
       this.setData({
            cartIds: options.cartIds,
            cartType: options.cartType
       })
       this.loadAddress()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
loadAddress: function () {
     let that = this
     var data = { 'memberId': app.globalData.userInfo.id }
     var promise = app.synPostData('address/select', data)
     promise.then(function (res) {
          that.setData({
               addresses: res.data.result
          })
     }); 
     },






     // 执行动画
     startAnimation: function (isShow, offset) {
          var that = this
          var offsetTem
          if (offset == 0) {
               offsetTem = offset
          } else {
               offsetTem = offset + 'rpx'
          }
          this.animation.translateY(offset).step()
          this.setData({
               animationData: this.animation.export(),
               isVisible: isShow
          })
     },




     // 点击所在地区弹出选择框
     selectDistrict: function (e) {
          var that = this
          if (that.data.addressMenuIsShow) {
               return
          }
          that.startAddressAnimation(true)
     },
     // 执行动画
     startAddressAnimation: function (isShow) {
          console.log(isShow)
          var that = this
          if (isShow) {
               that.animation.translateY(0 + 'vh').step()
          } else {
               that.animation.translateY(40 + 'vh').step()
          }
          that.setData({
               animationAddressMenu: that.animation.export(),
               addressMenuIsShow: isShow,
          })
     },
     // 点击地区选择取消按钮
     cityCancel: function (e) {
          this.startAddressAnimation(false)
     },
     // 点击地区选择确定按钮
     citySure: function (e) {
          var that = this
          var city = that.data.city
          var value = that.data.value
          that.startAddressAnimation(false)
          // 将选择的城市信息显示到输入框
          var areaInfo = that.data.provinces[value[0]].name + ',' + that.data.citys[value[1]].name + ',' + that.data.areas[value[2]].name
          that.setData({
               areaInfo: areaInfo,
          })
     },
     hideCitySelected: function (e) {
          console.log(e)
          this.startAddressAnimation(false)
     },
     // 处理省市县联动逻辑
     cityChange: function (e) {
          console.log(e)
          var value = e.detail.value
          var provinces = this.data.provinces
          var citys = this.data.citys
          var areas = this.data.areas
          var provinceNum = value[0]
          var cityNum = value[1]
          var countyNum = value[2]
          if (this.data.value[0] != provinceNum) {
               var id = provinces[provinceNum].id
               this.setData({
                    value: [provinceNum, 0, 0],
                    citys: address.citys[id],
                    areas: address.areas[address.citys[id][0].id],
               })
          } else if (this.data.value[1] != cityNum) {
               var id = citys[cityNum].id
               this.setData({
                    value: [provinceNum, cityNum, 0],
                    areas: address.areas[citys[cityNum].id],
               })
          } else {
               this.setData({
                    value: [provinceNum, cityNum, countyNum]
               })
          }
          console.log(this.data)
     },

})